package com.example.newdemo.thread;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.example.newdemo.R;
import com.example.newdemo.utils.LogUtils;

public class ThreadActivity extends Activity {

//    Handler handler=new Handler(){
//        @Override
//        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
//            LogUtils.showLog("arg1 = " + msg.arg1 + ";arg2 = " + msg.arg2 + ";obj = "+ msg.obj);
//        }
//    };

    Handler handler;


    private TextView tv;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_thread_demo);
//        init();

        testDemo();
    }

    private void init() {

        tv = findViewById(R.id.text_thread_act);

        postDemo();

//        handler.dispatchMessage();

        Thread thread = new Thread(){
            @Override
            public void run() {
                super.run();
                Message msg=new Message();
                msg.arg1=11;
                msg.arg2=22;
                msg.obj="333333333";

//                handler.sendMessage(msg);
                handler.dispatchMessage(msg);

                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        // 在这里进行UI操作
                        if (isMainThread()){
                            //在主线程中了
//                            LogUtils.showLog("this is main thread ");
                        }else {
//                            LogUtils.showLog("this is not main thread ");
                        }
                    }
                });

            }
        };

        thread.start();



    }


    private void testDemo(){
        Thread thread =new Thread(){
            @Override
            public void run() {
                super.run();
                Looper.prepare();
                handler = new Handler(){
                    @Override
                    public void handleMessage(Message msg) {
                        super.handleMessage(msg);

                        LogUtils.showLog("arg1 = " + msg.arg1 + ";arg2 = " + msg.arg2 + ";obj = "+ msg.obj);

                        if (isMainThread()){
                            LogUtils.showLog("this is main thread ");
                        }else {
                            LogUtils.showLog("this is not main thread ");
                        }

                    }
                };

                Looper.loop();


                Message msg=new Message();
                msg.arg1=11;
                msg.arg2=22;
                msg.obj="333333333";

                handler.sendMessage(msg);
//                handler.dispatchMessage(msg);
            }
        };

        thread.start();


    }

    private void postDemo() {

        Thread postThread = new Thread(){
            @Override
            public void run() {
                super.run();

                if (isMainThread()){
                    //在主线程中了
                    LogUtils.showLog("this is main thread  ***");
                }else {
                    LogUtils.showLog("this is not main thread *****");
                }


                tv.post(new Runnable() {
                    @Override
                    public void run() {

                        // 在这里进行UI操作
                        if (isMainThread()){
                            //在主线程中了
                            LogUtils.showLog("this is main thread  -------");
                        }else {
                            LogUtils.showLog("this is not main thread ---------");
                        }

                    }
                });

            }
        };

        postThread.start();





    }


    public boolean isMainThread() {
        return Looper.getMainLooper() == Looper.myLooper();
    }

}



